//
//  BGMessageTopMidView.m
//  bigMall
//
//  Created by LTOVE on 16/7/25.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMessageTopMidView.h"
#import "BGMessageNameAndValueModel.h"
@implementation BGMessageTopMidView

+ (UIView *)initWithFrame:(CGRect)frame andDataArray:(NSArray *)dataArray andUnit:(NSString *)unit
{
    BGMessageTopMidView * view = [[BGMessageTopMidView alloc]initWithFrame:frame];
    //使用九宫格添加视图
    UIView *smallView = [[UIView alloc]initWithFrame:CGRectMake(BGMaginLeft, 0, view.width - 2 * BGMaginLeft, view.height)];
    [view addSubview:smallView];
    view.backgroundColor = [UIColor whiteColor];
    [view setUpViewWith:smallView andDataArray:dataArray andUnit:unit];
//    设置竖线
    [view setUpLineViewWith:view];
    return view;
}

- (void)setUpLineViewWith:(UIView *)view
{
    UILabel * label = [UILabel new];
    label.height = BGTopMidViewHeigh - BGMaginTop - BGMaginButtom;
    label.width = 1;
    label.center = CGPointMake(view.width / 3.0, view.height * 0.5);
    UILabel *label2 = [UILabel new];
    label2.size = label.size;
    label2.center = CGPointMake(view.width * 2 / 3.0, view.height * 0.5);
//    label.backgroundColor = label2.backgroundColor = GWColor(220, 220, 220, 1);
    label.backgroundColor = label2.backgroundColor = [UIColor colorWithRed:220 green:220 blue:220 alpha:1];
    [view addSubview:label];
    [view addSubview:label2];
}

- (void)setUpViewWith:(UIView *)view andDataArray:(NSArray *)dataArray andUnit:(NSString *)unit
{
    int colCount = 3;
    CGFloat width = view.width / (CGFloat)colCount;
    CGFloat heigh = view.height;
    NSArray *unitArray = @[unit,@"元",@""];
    for (int i = 0; i < dataArray.count; i ++) {
        int row = i / colCount;
        int col = i % colCount;
        CGFloat x = col * width;
        CGFloat y = row * heigh;
        BGMessageNameAndValueModel *model = dataArray[i];
        UIView *views = [BGMessageTopMidView initWithFrame:CGRectMake(x, y, width, heigh) andTitle:model.title andSecTitle:model.value andUnit:unitArray[i]];
//        GWLog(@"%@",model.title);
        view.backgroundColor = [UIColor redColor];
//        [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        view.backgroundColor = GWColor(245, 245, 245, 1);
        [view addSubview:views];
    }
}

+ (UIView *)initWithFrame:(CGRect)frame andTitle:(NSString *)title andSecTitle:(NSString *)sectitle andUnit:(NSString *)unit
{
    UIView *view = [[UIView alloc]initWithFrame:frame];
    
    UILabel *titLabel = [[UILabel alloc]init];
    titLabel.text = title;
    titLabel.font = BGTitleSize;
    [titLabel setTextColor:[UIColor blackColor]];
    [titLabel sizeToFit];
    titLabel.center = CGPointMake(view.width * 0.5,(view.height - titLabel.height) * 0.5);
    [view addSubview:titLabel];
    
    UILabel *secTitleLabel = [[UILabel alloc]init];
    secTitleLabel.text = [NSString stringWithFormat:@"%@%@",sectitle,unit];
    secTitleLabel.font = BGTitleSize;
    secTitleLabel.textColor = [UIColor redColor];
    [secTitleLabel sizeToFit];
    secTitleLabel.center = CGPointMake(titLabel.centerX, (view.height + secTitleLabel.height) * 0.5);
    [view addSubview:secTitleLabel];
    
    return view;
}
@end
